package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    //查询角色列表
    @Override
    public PageResult<Role> roleList(String name, String label, Integer page, Integer pageSize) {
        //设置Page Helpper分页参数
        PageHelper.startPage(page, pageSize);
        //调用mapper接口，执行查询
        List <Role> roleList = roleMapper.selectList(name, label);
        //解析并分装分页结果
        Page<Role> p=(Page<Role>) roleList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    //根据ID删除用户
    @Override
    public void delete(Integer id) {
        roleMapper.delete(id);
    }
    //添加用户
    @Override
    public void add(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }
    //根据ID查询用户
    @Override
    public Role getByID(Integer id) {
        return roleMapper.selectByID(id);
    }
    //修改用户
    @Override
    public void upDate(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.upDate(role);
    }
    //查询所有角色
    @Override
    public Result allList(Role role) {
        List<Role> roleList = roleMapper.selectAllList();
        return Result.success(roleList);
    }
}
